package cn.inther.mock.controller;

import com.alibaba.fastjson.JSON;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/mock"})
@RestController
public class MockController {

  @RequestMapping
  public String mock(HttpServletRequest request, @RequestBody(required = false) Object data) {
    String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    System.out.println("---------------------" + format + "收到数据--------------------");
    Map resultMap = new HashMap();
    Map<String, String[]> parameterMap = request.getParameterMap();
    System.out.println(JSON.toJSONString(data));
    System.out.println(JSON.toJSONString(parameterMap));
    if (parameterMap != null && parameterMap.size() > 0) {
      if (parameterMap.get("returnData") != null) {
        return parameterMap.get("returnData")[0];
      } else {
        resultMap.put("param", JSON.toJSONString(parameterMap));
        resultMap.put("body", JSON.toJSONString(data));
        return JSON.toJSONString(resultMap);
      }
    } else {
      return JSON.toJSONString(data);
    }
  }

}
